import { Injectable } from '@nestjs/common';
import config from 'config/email';
import * as nodemailer from 'nodemailer';

@Injectable()
export default class MailService {
  private transporter: nodemailer.Transporter;

  constructor() {
    // 创建一个Nodemailer transporter实例
    this.transporter = nodemailer.createTransport({
      host: 'smtp.qq.com', // SMTP服务器主机
      port: 465, // SMTP服务器端口
      secure: true, // 使用 SSL
      auth: {
        user: config.email, // 发件邮箱
        pass: config.authKey, // 授权码
      },
    });
  }

  async sendVerificationCode(email: string, code: string) {
    const mailOptions: nodemailer.SendMailOptions = {
      from: config.email, // 发件邮箱
      to: email, // 收件邮箱
      subject: '验证码', // 邮件主题
      text: `您的验证码是: ${code}`, // 邮件正文
    };

    // 发送邮件
    await this.transporter.sendMail(mailOptions);
  }
}
